package com.controller;

import com.domain.SysDictEntity;
import com.dto.JsonResponse;
import com.service.SysDictService;
import com.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author sly
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/sysDict")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    /**
     * 根据字典值查询字典数据信息
     */
    @GetMapping(value = "/getByDictValue")
    public JsonResponse<SysDictEntity> getByDictValue(@RequestParam String dictValue) {
        SysDictEntity result = sysDictService.selectDictByValue(dictValue);
        return new JsonResponse<>(result);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/getByDictType")
    public JsonResponse<List<SysDictEntity>> getByDictType(@RequestParam String dictType) {
        List<SysDictEntity> result = sysDictService.selectDictByType(dictType);
        if (StringUtils.isNull(result)) {
            result = new ArrayList<>();
        }
        return new JsonResponse<>(result);
    }

    /**
     * 查询字典详情
     */
    @GetMapping("/getSysDictDetail")
    public JsonResponse<SysDictEntity> getSysDictDetail(@RequestParam Long sysDictId) {
        SysDictEntity result = sysDictService.getSysDictDetail(sysDictId);
        return new JsonResponse<>(result);
    }

    /**
     * 新增字典
     */
    @PostMapping("/addSysDict")
    public JsonResponse<String> addSysDict(@RequestBody SysDictEntity sysDict) {
        sysDictService.addSysDict(sysDict);
        return JsonResponse.success();
    }

    /**
     * 修改字典信息
     */
    @PostMapping("/updateSysDict")
    public JsonResponse<String> updateSysDict(@RequestBody SysDictEntity sysDict) {
        sysDictService.updateSysDict(sysDict);
        return JsonResponse.success();
    }

    /**
     * 删除字典
     */
    @GetMapping("/deleteSysDict")
    public JsonResponse<String> deleteSysDict(@RequestParam Long sysDictId) {
        sysDictService.deleteSysDict(sysDictId);
        return JsonResponse.success();
    }
}
